-- UIChannelSelectQC
-- Created by chengb Oct/14/2015
-- 青瓷登录方式选择窗口

require "game/ui/form/login/UIQCAccountRegister"
require "game/ui/form/login/UIQCAccountLogin"

UIChannelSelectQC = class("UIChannelSelectQC", function()
    return cc.Layer:create();
end);

function UIChannelSelectQC.create()
    return UIChannelSelectQC.new();
end

-- 构造函数
function UIChannelSelectQC:ctor()
    self:setName("UIChannelSelectQC");

    -- 初始化
    local node = cc.CSLoader:createNode("layout/login/android/ChannelSelectQC.csb");
    self:addChild(node);
    self.node = node;

    -- 注册点击事件
    self:registerTouchEvent();

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIChannelSelectQC:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 重绘
function UIChannelSelectQC:redraw()
    local regTipLabel = findChildByName(self.node, "CT/reg_tip");
    TextStyleM.setTextStyle(regTipLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_SKY_BLUE);
    regTipLabel:setString(getLocStr("register_account_tip"));
end

-- 进入通信显示模式
function UIChannelSelectQC:enterCommunicateMode()
    -- 关闭当前界面
    closeFormByName("UIChannelSelectQC");

    local uiLogin = UIMgr.getCurrentScene():getFormByName("UILogin");
    if uiLogin then
        uiLogin:enterCommunicateMode();
    end
end

-- 注册按钮点击事件
function UIChannelSelectQC:registerTouchEvent()
    -- 注册游客登录按钮点击事件
    local btnGuest = findChildByName(self.node, "CT/quick_login");
    local function onGuestClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 尝试进行游客登录
            self:tryGuestLogin();
        end
    end
    btnGuest:addTouchEventListener(onGuestClick);
    TextStyleM.setTextStyle(btnGuest, TextStyleM.TEXT_SIZE_SMALL2, TextStyleM.TEXT_COLOR_WHITE, false);
    btnGuest:setTitleText(getLocStr("quick_login"));

    -- 注册账号登录按钮点击事件
    local btnAccount = findChildByName(self.node, "CT/account_login");
    local function onAccountLoginClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            self:gotoAccountLogin();
        end
    end
    btnAccount:addTouchEventListener(onAccountLoginClick);
    TextStyleM.setTextStyle(btnAccount, TextStyleM.TEXT_SIZE_SMALL2, TextStyleM.TEXT_COLOR_WHITE, false);
    btnAccount:setTitleText(getLocStr("account_login"));

    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIChannelSelectQC");
        end
    end
    AddTouchEventListener(btnBack, onBackClick);
    AddBackEventHandler(self, btnBack, btnBack.touchEventListener);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));
    btnBack:setOpacity(0);

    -- 注册账号
    local regAccountLabel = findChildByName(self.node, "CT/reg_tip");
    regAccountLabel:setTouchEnabled(true);
    local function onRegClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 打开账号注册界面
            self:gotoRegister();
        end
    end
    regAccountLabel:addTouchEventListener(onRegClick);

    local goImg = findChildByName(self.node, "CT/go");
    goImg:setTouchEnabled(true);
    goImg:addTouchEventListener(onRegClick);

    -- 点击背景底图关闭
    local bgImg = findChildByName(self.node, "bg/bgImg");
    bgImg:setTouchEnabled(true);
    bgImg:addTouchEventListener(onBackClick);
end

-- 前往注册
function UIChannelSelectQC:gotoRegister()
    -- 隐藏当前界面
    self:setVisible(false);

    -- 打开注册界面
    UIMgr.getCurrentScene():removeFormByName("UIQCAccountRegister");
    local uiQCAccountRegister = UIQCAccountRegister.create();
    UIMgr.getCurrentScene():addForm(uiQCAccountRegister);
end

-- 账号登录
function UIChannelSelectQC:gotoAccountLogin()
    -- 隐藏当前界面
    self:setVisible(false);

    -- 打开青瓷账号登录界面
    UIMgr.getCurrentScene():removeFormByName("UIQCAccountLogin");
    uiForm = UIQCAccountLogin.create();
    UIMgr.getCurrentScene():addForm(uiForm);
end

-- 注册事件处理回调函数
function UIChannelSelectQC:registerEventCallback()
    self:registerScriptHandler(function(ev)
        if ev == "enter" then
            -- 显示登录界面的遮罩
            local uiLogin = UIMgr.getCurrentScene():getFormByName("UILogin");
            uiLogin:setMaskVisible(true);

        elseif ev == "exit" then
            -- 反注册所有事件
            EventMgr.removeAll("UIChannelSelectQC");

            -- 隐藏登录界面的遮罩
            local uiLogin = UIMgr.getCurrentScene():getFormByName("UILogin");
            if uiLogin then
                uiLogin:setMaskVisible(false);
            end
        end
    end);
end

-- 尝试进行游客登录
function UIChannelSelectQC:tryGuestLogin()
    local function okFunc()
        -- 尝试进行游客登录
        LoginM.tryGuestLogin();

        -- 进入通信显示
        local uiLogin = UIMgr.getCurrentScene():getFormByName("UILogin");
        if uiLogin then
            uiLogin:enterCommunicateMode();
        end
    end

    if getLastAccountChannel() ~= "" then
        -- 已经登录过游戏
        okFunc();
        return;
    end

    -- 从未登录过游戏，需要弹出提示
    confirm2(CONFIRM_TYPE_OK, getLocStr("guest_mode"), getLocStr("guest_mode_tip"), okFunc);
end







